package com.atguigu.study.cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class CASDemoo {
    private int number;

    public int getNumber() {
        return number;
    }

    public synchronized void setNumber() {
         ++number;
    }
    public AtomicInteger atomicInteger=new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
         atomicInteger.getAndIncrement();
         atomicInteger.incrementAndGet();
    }

    public static void main(String[] args) throws InterruptedException {
        CASDemoo casDemoo = new CASDemoo();
        CountDownLatch countDownLatch = new CountDownLatch(100);

        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                for (int i1 = 1; i1 <= 100; i1++) {
                    casDemoo.setNumber();
                    casDemoo.setAtomicInteger();
                }
                countDownLatch.countDown();
            },String.valueOf(i)).start();

        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t"+casDemoo.getNumber());
        System.out.println(Thread.currentThread().getName()+"\t"+casDemoo.getAtomicInteger());
    }
}
